import Vue from 'vue'
import routes from './routes'
import store from '@/store'
import VueRouter, { RouteConfig, RouterMode } from 'vue-router'
import { Store } from 'vuex'

const APP_NAME = process.env.APP_SCOPE_NAME
const isPortal = Vue.prototype.isPortal

let Router: RouteConfig[] | VueRouter = [{
    path: isPortal ? `/items/${APP_NAME}` : '/',
    component: () => import('components/index.vue'),
    children: routes
}]
if (!isPortal) {
    Vue.use(VueRouter)
    Router = new VueRouter({
        mode: process.env.VUE_ROUTER_MODE as RouterMode,
        // base: process.env.VUE_ROUTER_BASE,
        // scrollBehavior: () => ({ y: 0 }),
        routes: Router
    })

    Router.beforeEach((to, from, next) => {
        const path = window.location.pathname.split('/')
        if (path.length > 3) { // 云端服务
            (<Store<any>>store).commit('updateOrganization', path[1])
        }

        next()
    })
}

export default Router

